package manager;

import java.util.ArrayList;

import entity.Student;
import exception.ValidationError;

/**
 * @author huangjm.2012, huiwensong.2012
 * 
 */
public class StudentMgr {

	private DatabaseMgr dbMgr;

	public StudentMgr() {
		dbMgr = new DatabaseMgr();
	}

	public ArrayList<Student> retrieveAllStudents() {
		return RetrieveStudentUtil.retrieveMultipleStudentsForQuery(dbMgr,
				"select * from student order by userId");
	}

	public Student retrieveStudentForUserId(String userId)
			throws ValidationError {
		Student student = RetrieveStudentUtil.retrieveStudentForQuery(dbMgr,
				"select * from student where userId=? order by userId", userId);

		if (student == null) {
			throw new ValidationError("invalid userid");
		}

		return student;
	}

	public void addStudent(Student student) {
		dbMgr.openAndUpdateAndClose(
				"insert into student (userId, password, name, school, edollar) values (?,?,?,?,?)",
				student.getUserId(), student.getPassword(), student.getName(),
				student.getSchool(), student.getEdollar());
	}

	public void deleteAllStudents() {
		dbMgr.openAndUpdateAndClose("delete from student");
	}

	public void updateEdollarForStudent(double newAmount, String userId) {
		dbMgr.openAndUpdateAndClose(
				"update student set edollar=? where userId=?", newAmount,
				userId);
	}

}
